package com.hzdesk.media.koreabatchmatvector.contral;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;

import com.hzdesk.media.koreabatchmatvector.base.MyClientApplication;
import com.hzdesk.media.koreabatchmatvector.bean.PathBean;
import com.hzdesk.media.koreabatchmatvector.bean.SerPath;
import com.hzdesk.media.koreabatchmatvector.bean.SerPoint;
import com.hzdesk.media.koreabatchmatvector.config.AppConfig;
import com.hzdesk.media.koreabatchmatvector.inter.ISketchpadDraw;

import java.util.Date;

/**
 * function:自由画笔
 *
 * @author:Mr.Lee
 */
@SuppressLint("NewApi")
public class PenuCtl implements ISketchpadDraw {

    private Path m_path = new Path();
    private Paint m_paint = new Paint();
    private boolean m_hasDrawn = false;
    private float mX = 0, mY = 0;
    private static final float TOUCH_TOLERANCE = 4;
    private boolean status = false;
    private Path mPath;
    private String color;
    private int size;
    private SerPath mCurPath;
    private long time;
    private int num;
    private Context mContext;

    public PenuCtl(int penSize, String penColor,Context context) {
        mContext=context;
        m_paint.setAntiAlias(true);
        m_paint.setDither(true);
        m_paint.setColor(Color.parseColor("#" + penColor));
        m_paint.setStyle(Paint.Style.STROKE);
        m_paint.setStrokeJoin(Paint.Join.ROUND);
        m_paint.setStrokeCap(Paint.Cap.ROUND);
        m_paint.setStrokeWidth(penSize);
        this.color = penColor;
        this.size = penSize;
    }

    public PenuCtl(Paint paint, Path path) {
        m_paint = paint;
        mPath = path;
        status = true;
    }

    public PenuCtl(Paint paint) {
        m_paint = paint;
    }

    public void cleanAll() {
        // TODO Auto-generated method stub
        m_path.reset();
    }

    public void draw(Canvas canvas) {
        // TODO Auto-generated method stub
        // m_paint.setColor(SketchpadView.getStrokeColor());
        // m_paint.setStrokeWidth(SketchpadView.getStrokeSize());
        if (status) {
            canvas.drawPath(mPath, m_paint);
            mPath = null;
            status = false;
        }
        if (null != canvas) {
            // canvas.drawPoint(mX, mY,m_paint);
            canvas.drawPath(m_path, m_paint);

            // Log.i("PenuCtl", "Penu实例");
        }
    }

    public boolean hasDraw() {
        // TODO Auto-generated method stub
        return m_hasDrawn;
    }

    public void touchDown(float x, float y) {
        // TODO Auto-generated method stub
        m_path.moveTo(x, y);
        m_path.lineTo(x, y);
        mX = x;
        mY = y;
        num++;
        if (MyClientApplication.isRecording()) {
            time = getTime();
            mCurPath = new SerPath();
            mCurPath.mPoints.add(new SerPoint(x, y));
            mCurPath.mTimes.add(getTime());
            //mCurPath.mArray.put(getTime(), new SerPoint(x, y));
        }
    }

    public void touchMove(float x, float y) {
        // TODO Auto-generated method stub
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            m_path.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            m_hasDrawn = true;
            mX = x;
            mY = y;
            num++;
            m_path.lineTo(x, y);
        }
        if (MyClientApplication.isRecording()) {
            mCurPath.mPoints.add(new SerPoint(x, y));
            mCurPath.mTimes.add(getTime());
            //mCurPath.mArray.put(getTime(), new SerPoint(x, y));
        }

    }

    public void touchUp(float x, float y) {
        // TODO Auto-generated method stub
        m_path.lineTo(mX, mY);
        mX = x;
        mY = y;
        num++;
        Log.d("Penu", "size:" + num);
        if (MyClientApplication.isRecording() && MyClientApplication.getMouseUserDeviceinfoBean(mContext,MyClientApplication.getUserNum()).getDisMouse()!=1) {
            mCurPath.mPoints.add(new SerPoint(x, y));
            mCurPath.mTimes.add(getTime());
            Log.d("Penu", "size:" + mCurPath.mPoints.size());
            //mCurPath.mArray.put(getTime(), new SerPoint(x, y));
            MyClientApplication.setmPathBeanList(new PathBean(time, AppConfig.PAINT_PENU, color, size, mCurPath, MyClientApplication.getUserNum()));
        }


    }

    public long getTime() {
        return new Date().getTime() - MyClientApplication.getStartTime();
    }

}
